package com.example.swordkotlin

import java.util.*

object Test6 {

    @JvmStatic
    fun main(args: Array<String>) {

    }

    class ListNode(var `val`: Int) {
        var next :ListNode? = null
    }

    /**
     * 输入一个链表的头节点，按链表从尾到头的顺序返回每个节点的值（用数组返回）。
     * */

    fun printListFromTailToHead(head: ListNode?): IntArray {
        // write code here

        var listNode = head
        val stack = Stack<Int>()
        while (head != null) {
            stack.push(listNode?.`val`)
            listNode = listNode?.next
        }
        val ans = IntArray(stack.size)

        for (i in stack.indices) {
            ans[i] = stack.pop()
        }
        return ans
    }
}